import rasterio
from rasterio.windows import Window
from rasterio.warp import calculate_default_transform
from rasterio.enums import Resampling
from rasterio.vrt import WarpedVRT
import ast


def to_window(w):
    w = ast.literal_eval(w)
    return Window.from_slices(*w)

##################### 读取

path = "E:\\datasets\\spacenet-dataset\\spacenet\\SN6_buildings\\train\\AOI_11_Rotterdam\\PS-RGB\\SN6_Train_AOI_11_Rotterdam_PS-RGB_20190822075219_20190822075510_tile_8147.tif"

tif = rasterio.open(path)

##################### 查看信息

# # 将（行、列）坐标中的像素位置映射到（x、y）空间位置的仿射变换矩阵
# # | 0.50, 0.00, 596042.23|
# # | 0.00,-0.50, 5746471.83|
# # | 0.00, 0.00, 1.00|
# print(tif.transform)

# # 像素宽高
# print(tif.width)
# print(tif.height)

# # 世界坐标bbox
# print(tif.bounds)

# # 原点 +init=epsg:32631
# print(tif.crs)

##################### 用窗口读取数据

# win = "((489, 809), (572, 892))"

# Win = to_window(win)

# data = tif.read(window=Win, masked=True)

# # 存取新tif

# kwargs = tif.meta.copy()
# kwargs.update({
#     'height': Win.height,
#     'width': Win.width,
#     'transform': rasterio.windows.transform(Win, tif.transform)})

# dst = rasterio.open("E:\\datasets\\client-data\\rasterio\\SN6_Train_AOI_11_Rotterdam_PS-RGB_20190822075219_20190822075510_tile_8147_win.tif", 'w', **kwargs)

# dst.write(data)

##################### 缩小一倍并存取

# transform, width, height = calculate_default_transform(
#         tif.crs, tif.crs, tif.width, tif.height, *tif.bounds, 
#         resolution=(1, 1))

# print(transform)
# print(width)
# print(height)
        

# vrt_opts = {
#     'resampling': Resampling.nearest,
#     'crs': tif.crs,
#     'transform': transform,
#     'height': height,
#     'width': width,
# }

# vrt_raster = WarpedVRT(tif, **vrt_opts)

# vrt_data = vrt_raster.read(masked=True)

# kwargs = tif.meta.copy()
# kwargs.update({
#     'height': height,
#     'width': width,
#     'transform': transform})

# dst = rasterio.open("E:\\datasets\\client-data\\rasterio\\SN6_Train_AOI_11_Rotterdam_PS-RGB_20190822075219_20190822075510_tile_8147_half3.tif", 'w', **kwargs)

# dst.write(vrt_data)

##################### 缩小一倍，并裁剪原分辨率上一半大小的窗口

transform, width, height = calculate_default_transform(
        tif.crs, tif.crs, tif.width, tif.height, *tif.bounds, 
        resolution=(1, 1))
        

vrt_opts = {
    'resampling': Resampling.nearest,
    'crs': tif.crs,
    'transform': transform,
    'height': height,
    'width': width,
}

vrt_raster = WarpedVRT(tif, **vrt_opts)

# win = "((489, 809), (572, 892))"
win = "((244, 404), (286, 446))"

Win = to_window(win)

vrt_data = vrt_raster.read(masked=True, window=Win)

# kwargs = tif.meta.copy() # 不能是 vrt_raster.meta.copy()
# kwargs.update({
#     'height': Win.height,
#     'width': Win.width,
#     'transform': rasterio.windows.transform(Win, vrt_raster.transform)})
# dst = rasterio.open("E:\\datasets\\client-data\\rasterio\\SN6_Train_AOI_11_Rotterdam_PS-RGB_20190822075219_20190822075510_tile_8147_halfwin.tif", 'w', **kwargs)

# dst.write(vrt_data)





